package com.dhecp.project.system.service.impl;

import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.project.system.domain.SysHardWareRegisterInfo;
import com.dhecp.project.system.mapper.SysHardWareRegisterInfoMapper;
import com.dhecp.project.system.service.SysHardWareRegisterInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SysHardWareRegisterInfoServiceImpl
 * @Description TODO
 * @Author alula
 * @Date 2020-07-21 15:16
 */
@Service
public class SysHardWareRegisterInfoServiceImpl implements SysHardWareRegisterInfoService {

    @Autowired
    private SysHardWareRegisterInfoMapper sysHardWareRegisterInfoMapper;

    @Override
    public List<SysHardWareRegisterInfo> querySysHardWareRegisterInfoList(Map<String, Object> params) {
        return sysHardWareRegisterInfoMapper.findSysHardWareRegisterInfoList(params);
    }

    @Override
    public SysHardWareRegisterInfo querySysHardWareRegisterInfoById(String id) {
        return sysHardWareRegisterInfoMapper.findSysHardWareRegisterInfoById(id);
    }

    @Override
    public SysHardWareRegisterInfo findSysHardWareRegisterInfoByHardInfo(String hardInfo) {
        return sysHardWareRegisterInfoMapper.findSysHardWareRegisterInfoByHardInfo(hardInfo);
    }

    @Override
    public Boolean querySysHardWareRegisterInfoIsExistHardInfo(String hardInfo) {
        if (StringUtils.isEmpty(hardInfo)) {
            return false;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("hardInfo",hardInfo);
        int count = sysHardWareRegisterInfoMapper.findSysHardWareRegisterInfoCount(params);
        return  (count > 0) ? true :false;
    }

    @Override
    public AjaxResult updateSysHardWareRegisterInfoByHardInfo(String hardInfo, String rememberUser) {
        if(StringUtils.isEmpty(hardInfo)){
            return AjaxResult.error(MessageUtils.message("module.error.paramfail", hardInfo));
        }
        if(StringUtils.isEmpty(rememberUser)){
            return AjaxResult.error(MessageUtils.message("module.error.paramfail", rememberUser));
        }
        Map<String, Object> upParams = new HashMap<>();
        upParams.put("hardInfo",hardInfo);
        upParams.put("rememberUser",rememberUser);
        int count = sysHardWareRegisterInfoMapper.updateSysHardWareRegisterInfoByHardInfo(upParams);
        if(count > 0){
            return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
        }
        return AjaxResult.error(MessageUtils.message("module.success.iserror"));
    }

    @Override
    public AjaxResult insertSysHardWareRegisterInfo(String unId) {
        if (StringUtils.isEmpty(unId)) {
            return AjaxResult.error(MessageUtils.message("module.error.paramfail", unId));
        }
        SysHardWareRegisterInfo unRegInfo = sysHardWareRegisterInfoMapper.findSysHardWareUnRegisterInfoById(unId);
        if(unRegInfo == null){
            return AjaxResult.error(MessageUtils.message("hardware.register.autherror", unId));
        }
        SysHardWareRegisterInfo registerInfo = new SysHardWareRegisterInfo();
        registerInfo.setHostIP(unRegInfo.getHostIP());
        registerInfo.setHardInfo(unRegInfo.getHardInfo());
        registerInfo.setHostName(unRegInfo.getHostName());
        sysHardWareRegisterInfoMapper.insertSysHardWareRegisterInfo(registerInfo);
        sysHardWareRegisterInfoMapper.deleteSysHardWareUnRegisterInfoByHardInfo(registerInfo.getHardInfo());
//        sysHardWareRegisterInfoMapper.deleteSysHardWareUnRegisterInfoById(unId);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    @Override
    public AjaxResult deleteSysHardWareRegisterInfo(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error(MessageUtils.message("module.error.paramfail", id));
        }
        sysHardWareRegisterInfoMapper.deleteSysHardWareRegisterInfoById(id);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    @Override
    public List<SysHardWareRegisterInfo> querySysHardWareUnRegisterInfoList(Map<String, Object> params) {
        return sysHardWareRegisterInfoMapper.findSysHardWareUnRegisterInfoList(params);
    }

    @Override
    public AjaxResult insertSysHardWareUnRegisterInfo(SysHardWareRegisterInfo sysHardWareRegisterInfo) {
        if (sysHardWareRegisterInfo == null) {
            return AjaxResult.error(MessageUtils.message("module.error.paramfail", sysHardWareRegisterInfo));
        }
        sysHardWareRegisterInfoMapper.deleteSysHardWareUnRegisterInfoByHardInfo(sysHardWareRegisterInfo.getHardInfo());
        sysHardWareRegisterInfoMapper.insertSysHardWareUnRegisterInfo(sysHardWareRegisterInfo);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

    @Override
    public AjaxResult deleteSysHardWareUnRegisterInfo(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error(MessageUtils.message("module.error.paramfail", id));
        }
        sysHardWareRegisterInfoMapper.deleteSysHardWareUnRegisterInfoById(id);
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }
}
